package com.plate.modules.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plate.common.core.result.Result;
import com.plate.modules.system.domain.bo.SysDictQueryBO;
import com.plate.modules.system.domain.bo.SysDictSaveBO;
import com.plate.modules.system.domain.po.SysDict;
import com.plate.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName SysDictController
 * @Description TODO
 * @Author jiaxd
 * @Date 2023年11月05日 19:05
 * @Version 1.0
 */

@RestController
@RequestMapping("/sys/sysDict")
public class SysDictController {

    @Autowired
    private ISysDictService sysDictService;

    @PostMapping("/list")
    public Result list(@RequestBody SysDictQueryBO queryBO) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        Page<SysDict> page = new Page<>(queryBO.getPage(), queryBO.getSize());
        Page<SysDict> dictPage = sysDictService.page(page, wrapper);
        return Result.SUCCESS(dictPage);
    }

    @PostMapping("/update")
    public Result update(@RequestBody SysDictSaveBO saveBO) {
        if (StrUtil.isBlankIfStr(saveBO.getId())) {
            SysDict sysDict = new SysDict();
            BeanUtils.copyProperties(saveBO, sysDict);
            sysDictService.isSave(sysDict);
        } else {
            SysDict sysDict = sysDictService.getById(saveBO.getId());
            BeanUtils.copyProperties(saveBO, sysDict);
            sysDictService.isUpdate(sysDict);
        }
        return Result.SUCCESS();
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Long id) {
        SysDict sysDict = sysDictService.getById(id);
        if (sysDict == null) {
            return Result.FAIL("");
        }
        boolean remove = sysDictService.removeById(id);
        return Result.SUCCESS(remove);
    }

}
